home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: How to use assert( )
- Date: 10 Apr 1996 14:10:11 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4kh83jINN2js@keats.ugrad.cs.ubc.ca>
- References: <4kc3k7$dur@orion.cybercom.net> <DpnqFE.E8v@eskimo.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <DpnqFE.E8v@eskimo.com>, Steve Summit <scs@eskimo.com> wrote:
- >In article <4kc3k7$dur@orion.cybercom.net>, nield@cybercom.net writes:
- >> I'm just starting my first project big enough to split among many
- >> people, and from the vague explanations I've heard, assert is supposed
- >> to be a usefull way to cause errors when someone passes your code bad
- >> values.
- >
- >Indeed (as long as we understand that "someone" is one of your
- >fellow programmers, and *not* one of the program's users).
- >
- >An assertion (which is what C's assert() macro implements)
- >allows you to document the assumptions made by a piece of code,
- >and furthermore to arrange that the code test its assumptions
- >as it runs. Usually, these assumptions boil down to "the rest
- >of the program is written correctly," and in a large program,
- >particularly one worked on by many people, this is of course
- >*not* always a valid assumption, and so is eminently worth
- >testing.
-
- The assumptions boil down to WP's---weakest preconditions. These are the
- sufficient initial conditions for the correct computation of a result by a
- given statement or statements.
-
- For example, the weakest precondition for having x == 5 after the statement
- x += 2; is x == 3. Before executing the statement, we can assert (x == 3).
-
- This is trivial, of course, but you can devise an arbitrarily complex system
- of correctness proof around this.
-
- I suppose that one could also use assert() in conjuction with loop invariants.
- If I think I have identifed the loop invariant of some iterative algorithm, why
- not assert() the sucker in the implementation? If it fails, either I have
- incorrectly identified the loop invariant or there is a bug in the
- implementation of the algorithm.
-
- (Kaz is going to go assert() crazy now...)
- --
-
-